//
//  MineInfoViewController.m
//  WeCommunity
//
//  Created by mac on 2017/6/15.
//  Copyright © 2017年 lzy. All rights reserved.
//

#import "MineInfoViewController.h"

#import "PhotoAlbumViewController.h"
#import "PhotoAlbumModel.h"
#import "ModifyUserinfoViewController.h"
#import "ModifyUserSexViewController.h"

@interface MineInfoViewController ()


@end

@implementation MineInfoViewController

-(void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark - get
-(MineInfoView *)mineInfoView
{
    if(!_mineInfoView){
        _mineInfoView = [[MineInfoView alloc] initWithFrame:CGRectMake(0, 0, KScreen_Width, KScreen_Height - KNaviheight - KTabBarHeight)];
        _mineInfoView.delegate = self;
    }
    _mineInfoView.contact = [UserInfoManger shared].contact;
    return _mineInfoView;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initWithUserInterface];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(userInfoMangerUpdateUserData:) name:USERLOGIN_CHANGE_NOTIKEY object:nil];
}

-(void)initWithUserInterface
{
    self.title = KNSLocal(@"mineInfoTitle");
    
    self.view.backgroundColor = [UIColor colorWithHex:0xf8f8f8 alpha:1.0];
    
    [self.view addSubview:self.mineInfoView];
    [self.mineInfoView reloadWithData];

}

#pragma mark - 用户数据更新
-(void)userInfoMangerUpdateUserData:(NSNotification *)noti
{
    [self.mineInfoView reloadWithData];
}

#pragma mark - contentListView delegate
-(void)mineCotentListViewSelectRowAtIndex:(NSInteger)index
{
    switch (index) {
        case 0:
        {
            [self modifyMineheadImage];
        }
            break;
        case 1:
        {
            [self modifyMoreInfomatinWithType:ModifyNickname];
        }
            break;
        case 2:
        {
            [self modifyMoreInfomatinWithType:ModifyWXUsername];
        }
            break;
        case 3:
        {
            [self modifyMoreInfomatinWithType:ModifyUserSex];
        }
            break;

        default:
            break;
    }
}

#pragma mark - 更换头像
-(void)modifyMineheadImage
{
    //相册
    PhotoAlbumViewController * photoAlbumVC = [[PhotoAlbumViewController alloc] init];
    photoAlbumVC.maxPhotoCount = 1;
    
    RootNaviViewController * nav = [[RootNaviViewController alloc] initWithRootViewController:photoAlbumVC];
    nav.navigationBar.translucent = NO;//设置不透明
    nav.navigationBar.barTintColor = NAVC_COLOR; //导航条颜色
    nav.navigationBar.tintColor = [UIColor whiteColor];  //导航条字体颜色
    [self presentViewController:nav animated:YES completion:nil];
    
    [photoAlbumVC photoAlbumChooseComplete:^(NSMutableArray *results) {
        //选完照片
        PhotoAlbumModel * albumModel = results.firstObject;
        if (!albumModel.fullScreenImage) {
            UIImage *image = [UIImage imageWithCGImage:[[albumModel.asset defaultRepresentation] fullScreenImage]];
            albumModel.fullScreenImage = image;
        }
        [[UserInfoManger shared] saveHeadImage:albumModel.fullScreenImage complete:^(BOOL success,NSString * error) {
            if (!success) {
                [UITool showHudWithText:error withView:self.view];
            }
        }];
    }];
}

#pragma mark - 修改名字和微信号
-(void)modifyMoreInfomatinWithType:(ModifyMineInfoType)type
{
    if (type == ModifyUserSex) {
        ModifyUserSexViewController * modifyVC = [[ModifyUserSexViewController alloc] init];
        [self.navigationController pushViewController:modifyVC animated:YES];
    }else{
        ModifyUserinfoViewController * modifyVC = [[ModifyUserinfoViewController alloc] init];
        modifyVC.modifyMineInfoType = type;
        [self.navigationController pushViewController:modifyVC animated:YES];
    }
}

@end
